/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.max.oti.system.bean;

import com.max.oti.system.service.interfac.IGlobalService;
import com.max.oti.system.util.FacesUtil;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import org.omnifaces.util.Faces;

/**
 *
 * @author max
 */
@ManagedBean
@ViewScoped
public class LoginBean implements Serializable{

    private Integer idSelectedRadio = 1;
    @ManagedProperty("#{globalService}")
    IGlobalService globalService;
    

    public void logginProcess() {
       
        try {
            if (idSelectedRadio == null) {
                FacesUtil.addMessage("Debe seleccionar una opción de ingreso", null, FacesMessage.SEVERITY_WARN);
            } else {
                if (idSelectedRadio == 1) {
                    int hashCode = FacesUtil.encryptUser("usuario", "password");
                    System.out.println(hashCode);
                    List<Integer> listCount = globalService.listHQL("select u.id from Equipo u where u.password='"
                            + hashCode + "' and u.usuario='" + "usuario" + "'");
                    if (listCount.isEmpty()) {
                        FacesUtil.addMessage("No existe usuario", null, FacesMessage.SEVERITY_WARN);
                    } else {
                        System.out.println("Ingreso");
                        Faces.getSession().setAttribute(FacesUtil.ATTRIBUTE_ID, listCount.get(0));
                        Faces.getSession().setAttribute(FacesUtil.ATTRIBUTE_USER_NAME, "emcripta el nombre");
                        Faces.redirect("index.jsf");
                    }
                } else {
                }
            }

        } catch (Exception ex) {
            Logger.getLogger(LoginBean.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public IGlobalService getGlobalService() {
        return globalService;
    }

    public void setGlobalService(IGlobalService globalService) {
        this.globalService = globalService;
    }

    public Integer getIdSelectedRadio() {
        return idSelectedRadio;
    }

    public void setIdSelectedRadio(Integer idSelectedRadio) {
        this.idSelectedRadio = idSelectedRadio;
    }
    
}
